<?php


namespace app\api\model;


use think\Cache;
use think\Exception;
use think\Model;

class MeetConfigModel extends Model
{
    // 表名
    protected $name = 'meet_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    //获得应用jsapi_ticket
    public static function getAppTicket($access_token)
    {
        $url  = 'https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token='.$access_token.'&type=agent_config';
        $client = new \GuzzleHttp\Client();
        $result = $client->get($url)->getBody()->getContents();
        $api_data = json_decode($result, true);
        //print_r($api_data);die();
        if($api_data['errmsg'] != 'ok'){
            log_print('获取access_token出错');
            log_print($api_data);
            return '';
        }
        return $api_data['ticket'];
    }

    //获得企业 jsapi_ticket
    public static function getTicket($access_token)
    {
        /*$qy_jsapi_ticket = Cache::get('qy_jsapi_ticket');
        if($qy_jsapi_ticket){
            return $qy_jsapi_ticket;
        }*/
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token='.$access_token;
        $client = new \GuzzleHttp\Client();
        $result = $client->get($url)->getBody()->getContents();
        $api_data = json_decode($result, true);
        //print_r($api_data);die();
        if($api_data['errmsg'] != 'ok'){
            log_print('获取access_token出错');
            log_print($api_data);
            return '';
        }
        /*
         * Array
            (
                [errcode] => 0
                [errmsg] => ok
                [ticket] => kgt8ON7yVITDhtdwci0qeUj7W5Dh-1veJvJD_BOZFxACWmgp7sxlGrxmCqjemkxgI4Xw0Z6IJOiYEriZxwunbg
                [expires_in] => 7200
            )
         *
         * */
        //Cache::set('qy_jsapi_ticket',$api_data['ticket'],7200);
        return $api_data['ticket'];
    }

    public static function getAccessToken($corpid,$secret)
    {
        $qy_access_token = Cache::get('qy_access_token');
        if($qy_access_token){
           // return $qy_access_token;
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$secret";
        $client = new \GuzzleHttp\Client();
        $result = $client->get($url)->getBody()->getContents();
        $api_data = json_decode($result, true);
        //print_r($api_data);die();
        if($api_data['errmsg'] != 'ok'){
            log_print('获取access_token出错');
            log_print($api_data);
            return '';
        }
        /*
                  * Array
         (
             [errcode] => 0
             [errmsg] => ok
             [access_token] => e9qoLaf96xFMOanXG8OwlEPS8moIFBCyp2OAa0fjRePgw2j99nposUE51Wj9mM40oBEledeYv5tkMx6oZivNX19s7F8Fhny2R-0kA7zsBQUqm-C-FD6lWu9-n1-0yyxu7kg-TzG6voGC7v-mr5i-6Kk1HeyP3VnQpX3vyec6zoqaydkPJTBZ1Wv5Nn3BewY8LvbQNNyLv6jpF7NZY2Xq-g
             [expires_in] => 7200
         )

      * */
        Cache::set('qy_access_token',$api_data['access_token'],7200);
        return $api_data;
    }


    public static function getId($id)
    {
        $row = self::get($id);
        return $row['AgentId'];
    }

    public static function config($AgentId = '1000011')
    {
        $a = input('AgentId');
        if(!empty($a)){
            $AgentId = input('AgentId');
        }

        $model = self::get(['AgentId' => $AgentId]);
        if(empty($model)){
            new Exception('参数错误');
        }
        $data = [
            'corpid' => self::get(2)['field_value'],
            'AgentId' => $model['AgentId'],
            'Secret' => $model['Secret'],
        ];
        return $data;
    }

}
